package com.meirengu.webview.controller;

import com.meirengu.model.Result;
import com.meirengu.utils.ValidatorUtil;
import com.meirengu.webview.service.LegalAdviceService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.meirengu.common.StatusCode.*;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 法务咨询相关接口
 */
@RestController
public class LegalAdviceControll extends BaseController {

    private Logger logger = LoggerFactory.getLogger(LegalAdviceControll.class);

    @Autowired
    private LegalAdviceService legalAdviceService;


    /**
     * 用户注册，只需要手机号
     */
    @RequestMapping(value = "/legal/register", method = GET)
    public Result register(@RequestParam("phoneNumber") String phoneNumber) {

        logger.info("/legal/register request params, phoneNumber:{}", phoneNumber);

        if (StringUtils.isEmpty(phoneNumber) || !ValidatorUtil.isMobile(phoneNumber)) {
            return super.setResult(MOBILE_FORMAT_ERROR, null, codeMsgMap.get(MOBILE_FORMAT_ERROR));
        }

        int i = legalAdviceService.register(phoneNumber);
        int code = i == 1 ? OK : REGISTER_IS_FAILED;
        return super.setResult(code, null, codeMsgMap.get(code));
    }

}
